package me.flyray.bsin.server.handler;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import me.flyray.bsin.facade.service.AccountService;
import me.flyray.bsin.infrastructure.biz.TransactionBiz;
import me.flyray.bsin.infrastructure.biz.WalletAccountBiz;
import me.flyray.bsin.mq.enums.MqEventCode;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * @author bolei
 * @date 2024/5/20
 * @desc
 */

@Slf4j
@Component
@RocketMQMessageListener(consumerGroup = "${rocketmq.consumer.group}", topic = "${rocketmq.consumer.topic}")
public class WaasMQConsumerHandler implements RocketMQListener<String> {

    @Autowired
    private WalletAccountBiz walletAccountBiz;
    @Autowired
    private TransactionBiz transactionBiz;
    @DubboReference(version = "${dubbo.provider.version}")
    private AccountService accountService;

    /**
     * 1、处理钱包创建消息
     * 2、处理加油确认消息
     * 3、确认交易确认消息
     * 4、处理CRM充值消息
     * @param message
     */
    @Override
    public void onMessage(String message) {
        try {
            JSONObject mQMsg = JSON.parseObject(message);
            MqEventCode eventCode = MqEventCode.getInstanceById((String) mQMsg.get("eventCode"));

            switch (eventCode){
                case CREATE_MPC_WALLET:
                    log.debug("处理创建钱包消息");
                    walletAccountBiz.getAppChainWalletAddress(mQMsg);
                    break;
                case GET_GAS_NOTIFY:
                    log.debug("处理gas加油消息");
                    try {
                        transactionBiz.handleGasTransactionConfirmationEvent(mQMsg);
                    } catch (Exception e) {
                        log.error("处理gas加油消息失败", e);
                    }
                    break;
                case CASH_CONCENTRATION_NOTIFY:
                    log.debug("处理资金归集消息");
                    transactionBiz.handleFundCollectionConfirmationEventNotify(mQMsg);
                    break;
                case CRM_RECHARGE_NOTIFY:
                    log.debug("处理CRM充值消息");
                    handleCrmRechargeEventMessage(mQMsg);
                    break;
                default:
                    log.warn("未知的消息类型: {}", eventCode);
                    break;
            }
        } catch (Exception e) {
            log.error("处理RocketMQ消息失败: {}", message, e);
        }
    }

    /**
     * 处理CRM充值事件消息
     */
    private void handleCrmRechargeEventMessage(JSONObject mQMsg) {
        try {
            // 构建充值请求参数
            java.util.Map<String, Object> reqMap = new java.util.HashMap<>();
            reqMap.put("bizRoleType", mQMsg.getString("bizRoleType"));
            reqMap.put("bizRoleTypeNo", mQMsg.getString("bizRoleTypeNo"));
            reqMap.put("tenantId", mQMsg.getString("tenantId"));
            reqMap.put("ccy", mQMsg.getString("ccy"));
            reqMap.put("category", mQMsg.getString("category"));
            reqMap.put("amount", new BigDecimal(mQMsg.getString("amount")));
            reqMap.put("decimals", mQMsg.getString("decimals"));
            reqMap.put("remark", mQMsg.getString("remark"));

            // 调用CRM服务进行充值
            accountService.recharge(reqMap);
            log.info("CRM用户充值成功: tenantId={}, amount={}", 
                    mQMsg.getString("tenantId"), mQMsg.getString("amount"));

        } catch (Exception e) {
            log.error("处理CRM充值消息失败: tenantId={}, amount={}, error={}", 
                    mQMsg.getString("tenantId"), mQMsg.getString("amount"), e.getMessage(), e);
        }
    }
}

